<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '统计分析管理/出入库统计/供应商商品出入库统计',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

    <!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
        $(".js-sidebar-nav>li:nth-child(11)>ul, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(3)>ul").show(80);
        $(".js-sidebar-nav>li:nth-child(11)>a>span, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(3)>a>span").addClass("sidebar-nav-sub-ico-rotate");
        $(".js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(3)>a, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(3)>ul>li:nth-child(2)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper statement">

        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">供应商商品出入库统计</div>
                        </div>
                        <div class="widget-body  am-fr">
                            <div class="am-u-sm-6 am-u-md-6 am-u-lg-6">
                                <div class="am-btn-toolbar">
                                    <div class="am-g">
                                        <div class="am-u-sm-12 am-u-md-6 am-form-group">
                                            <button type="button" class="am-btn am-btn-default am-margin-right"
                                                    id="my-start">开始日期
                                            </button>
                                            <span id="my-startDate"></span>
                                        </div>
                                        <div class="am-u-sm-12 am-u-md-6 am-form-group">
                                            <button type="button" class="am-btn am-btn-default am-margin-right"
                                                    id="my-end">结束日期
                                            </button>
                                            <span id="my-endDate"></span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-6 am-u-md-6 am-u-lg-6">

                                <div class="am-u-sm-12 am-u-md-12 am-u-lg-6">
                                    <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                        <!--                                    <form action="/admin/user/search" method="get">-->
                                        <input type="text" class="am-form-field" name="search" id="searchValue" placeholder="请输入供应商名称">
                                        <span class="am-input-group-btn">
                                            <a href="javascript:void(0)" id="search"
                                               class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>
                                        </span>
                                        <!--                                    </form>-->
                                    </div>
                                </div>

                            </div>
                            <div class="am-u-sm-12 am-form-group ">
                                <div class="am-btn-toolbar am-u-sm-12">
                                    <div class=" am-btn-group-xs">
                                        <?php
                                        $type = isset($_GET['type']) ? $_GET['type'] : 'put';
                                        ?>
                                        <a href="javascript:void(0);" onclick="put()"
                                           class="am-btn   <?php if ($type == 'put') {
                                               echo 'am-btn-primary';
                                           } else {
                                               echo 'am-btn-default';
                                           } ?> ">商品入库</a>
                                        <a href="javascript:void(0);" onclick="out()"
                                           class="am-btn  <?php if ($type == 'out') {
                                               echo 'am-btn-primary ';
                                           } else {
                                               echo 'am-btn-default';
                                           } ?> ">商品出库</a>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-12 am-form-group ">
                                <div class="am-btn-toolbar am-u-sm-12">
                                    <div class=" am-btn-group-xs">
                                        <a href="javascript:void(0);" onclick="" class="am-btn am-btn-primary"><span
                                                    class="am-icon-download"></span> 导出</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="am-u-sm-12 searchList">
                            <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black "
                                   id="example-r">
                                <thead>
                            <?php
                                if(isset($putlist) && $putlist){
                                    echo '<tr>
                                <th>序号</th>
                                <th>供应商编码</th>
                                <th>供应商名称</th>
                                <th>供应商分类</th>
                                <th>入库数量</th>
                                <th>入库金额</th>
                                <th>入库人员</th>
                            </tr>';
                                }else if(isset($outlist) && $outlist){
                                    echo '<tr>
                                <th>序号</th>
                                <th>供应商编码</th>
                                <th>供应商名称</th>
                                <th>供应商分类</th>
                                <th>出库数量</th>
                                <th>出库金额</th>
                                <th>出库人员</th>
                            </tr>';
                                }
                            ?>
                                </thead>

                                <tbody class="choose-box">

                            <?php
                            if (isset($putlist) && $putlist) {
                                //2物料包、3书籍、4机器耗材、9打印机租赁、8供应商商品
                                foreach ($putlist as $k=>$v) {
                                    // 因为供应商能查询到才有supplierId 和 code 所有用来此判断
                                    if(isset($v['supplierId']) && isset($v['code'])){
                                        echo '<tr>';
                                        echo'<td>' . $v['supplierId'] . '</td>';
                                        echo '<td><a href="/admin/statement/outAndPutBySupplierPutDetails?supplierId='.$v['supplierId'].'">' . $v['code'] . '</a></td>';

                                        echo '<td>' . $k . '</td>
                                                <td>' .   $v['type'] . '</td>
                                            <td>' . array_sum($v['quantity']) . '</td>
                                            <td>' . array_sum($v['priceAll']) . '</td>
                                            <td>' . $v['createMan'] . '</td>';

                                        echo    '</tr>';
                                    }

                                }
                            }else if(isset($outlist) && $outlist){
                                foreach ($outlist as $k2=>$v2) {
                                    // 因为供应商能查询到才有supplierId 和 code 所有用来此判断
                                    if(isset($v2['supplierId']) && isset($v2['code'])) {
                                        echo '<tr>';
                                        echo '<td>' . $v2['supplierId'] . '</td>';
                                        echo '<td><a href="/admin/statement/outAndPutBySupplierOutDetails?supplierId=' . $v2['supplierId'] . '">' . $v2['code'] . '</a></td>';

                                        echo '<td style="width: 180px;">' . $k2 . '</td><td>' . $v2['type'] . '</td>
                                            <td>' . array_sum($v2['quantity']) . '</td>
                                            <td>' . array_sum($v2['priceAll']) . '</td>
                                            <td>' . $v2['createMan'] . '</td>';

                                        echo '</tr>';
                                    }
                                }
                            }
                            ?>

                        </div>
                        <div class="am-u-lg-12">
                            <div class="am-u-sm-centered">
                                <ul class="am-pagination am-pagination-centered" id="paging">

                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/page.js"></script>
<script>
    $(function () {

        var startDate = new Date(2014, 11, 20);
        var endDate = new Date(2014, 11, 25);
        var $alert = $('#my-alert');
        $('#my-start').datepicker().on('changeDate.datepicker.amui', function (event) {
            if (event.date.valueOf() > endDate.valueOf()) {
                $alert.find('p').text('开始日期应小于结束日期！').end().show();
            } else {
                $alert.hide();
                startDate = new Date(event.date);
                $('#my-startDate').text($('#my-start').data('date'));
            }
            $(this).datepicker('close');
        });

        $('#my-end').datepicker().on('changeDate.datepicker.amui', function (event) {
            if (event.date.valueOf() < startDate.valueOf()) {
                $alert.find('p').text('结束日期应大于开始日期！').end().show();
            } else {
                $alert.hide();
                endDate = new Date(event.date);
                $('#my-endDate').text($('#my-end').data('date'));
            }
            $(this).datepicker('close');
        });
    });
</script>

<script>
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)return unescape(r[2]);
        return null;
    }
    function put() {
        var search = GetQueryString('search');
        // $(".am-btn-primary").removeClass("am-btn-primary");
        // $(this).addClass("am-btn-primary");

        if (search == null) {
            window.location.href = '?page=1' + '&type=put'
        } else {
            window.location.href = '?page=1' + '&search=' + search + '&type=put'
        }
    }
    function out() {
        var search = GetQueryString('search');
        if (search == null) {
            window.location.href = '?page=1' + '&type=out'
        } else {
            window.location.href = '?page=1' + '&search=' + search + '&type=out'

        }
    }
</script>
<script>

    //类型传参 type == 类型， search == 要搜索的
    $("#search").on("click",function () {
        var search = $("#searchValue").val();
        var page = "<?php echo isset($_GET['page']) ? $_GET['page'] : 1 ?>";
        var type = "<?php echo isset($_GET['type']) ? $_GET['type'] : 'put' ?>";
        var startTime = $("#my-startDate").text();
        var endTime = $("#my-endDate").text();
        chooseCheck(type,search,page,startTime,endTime);
    });

    //筛选日期
    var startDate = new Date(2014, 11, 20);
    var endDate = new Date(2100, 11, 25);
    var $alert = $('#data-alert');
    $('#my-start').datepicker().on('changeDate.datepicker.amui', function(event) {

        if (event.date.valueOf() > endDate.valueOf()) {
            $alert.find('p').text('开始日期应小于结束日期！').end().show();
            return false;

        } else {
            $alert.hide();
            startDate = new Date(event.date);
            $('#my-startDate').text($('#my-start').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        search = $("#searchValue").val();
        page = "<?php echo isset($_GET['page']) ? $_GET['page'] : 1 ?>";
        type = "<?php echo isset($_GET['type']) ? $_GET['type'] : 'put' ?>";

        console.log(1231231231);
        chooseCheck(type,search,page,startTime,endTime);

    });
    $('#my-end').datepicker().on('changeDate.datepicker.amui', function(event) {

        if (event.date.valueOf() < startDate.valueOf()) {
            $alert.find('p').text('结束日期应大于开始日期！').end().show();
            return false;
        } else {
            $alert.hide();
            endDate = new Date(event.date);
            $('#my-endDate').text($('#my-end').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        search = $("#searchValue").val();
        page = "<?php echo isset($_GET['page']) ? $_GET['page'] : 1 ?>";
        type = "<?php echo isset($_GET['type']) ? $_GET['type'] : 'put' ?>";

        chooseCheck(type,search,page,startTime,endTime);

    });

    function chooseCheck(type,search,page,startTime,endTime) {
        $.ajax({
            type: "GET",
            url : "/api/statement/outAndPutBySupplierSearch",
            data : {
                type : type,
                search : search,
                page: page,
                startTime:startTime,
                endTime: endTime
            },
            dateType : "json",
            success : function (msg) {
                var data = JSON.parse(msg);
                var list = '';



                if(data.error ==  true){
                    $('.searchList').nextAll().remove();


                    $(".searchList").html(data.info);
                    return false;
                }

                var sum = 0;

                // 计算数组的总和
                function arraySum(item) {
                    var nummber = null;
                    for(var i=0;i<item.length;i++){
                        nummber += item[i] - 0;
                    }
                    return nummber;
                }
                // 格式化时间撮
                function formatDateTime(inputTime) {
                    var date = new Date(inputTime * 1000);
                    var y = date.getFullYear();
                    var m = date.getMonth() + 1;
                    m = m < 10 ? ('0' + m) : m;
                    var d = date.getDate();
                    d = d < 10 ? ('0' + d) : d;
                    return y + '-' + m + '-' + d;
                }

                switch (data.type){
                    case 'put':
                        list += '<table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black " id="example-r"><thead>'
                            + '<tr>'
                            +' <th>序号</th> '
                            + '<th>供应商编码</th>'
                            + '<th>供应商名称</th>'
                            + '<th>供应商分类</th>'
                            + '<th>入库数量</th>'
                            + '<th>入库金额</th>'
                            + '<th>入库人员</th>'
                            + '</tr>'
                            + '</thead>'
                            + '<tbody class="choose-box">';
                        for(var key in data[0]){
                            var quantity = data[0][key].quantity;
                            var priceAll = data[0][key].priceAll;

                            list += "<tr>"
                                +  '<td>'+ data[0][key].supplierId +'</td>'
                                +  '<td><a href="/admin/statement/outAndPutBySupplierPutDetails?supplierId="'+ data[0][key].supplierId +'>'+ data[0][key].code +'</a></td>'
                                +  '<td style="width: 180px;">'+ key +'</td>'
                                +  '<td>'+ data[0][key].type +'</td>'
                                +  '<td>'+ arraySum(quantity) +'</td>'
                                +  '<td>'+ arraySum(priceAll) +'</td>'
                                +  '<td>'+ data[0][key].createMan +'</td>'
                                + '</tr>';
                        }

                        list += '</tbody>'
                            + '</table>';
                        break;
                    case 'out':
                        list += ' <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black " id="example-r"><thead>'
                            + '<tr>'
                            +' <td>序号</td> '
                            + '<td>供应商编码</td>'
                            + '<td>供应商名称</td>'
                            + '<td>供应商分类</td>'
                            + '<td>出库数量</td>'
                            + '<td>出库金额</td>'
                            + '<td>出库人员</td>'
                            + '</tr>'
                            + '</thead>'
                            + '<tbody class="choose-box">';
                        for(var key2 in data[0]){
                            var quantity2 = data[0][key2].quantity;
                            var priceAll2 = data[0][key2].priceAll;

                            list += "<tr>"
                                +  '<td>'+ data[0][key2].supplierId +'</td>'
                                +  '<td><a href="/admin/statement/outAndPutBySupplierOutDetails?supplierId="'+ data[0][key2].supplierId +'>'+ data[0][key2].code +'</a></td>'
                                +  '<td style="width: 180px;">'+ key2 +'</td>'
                                +  '<td>'+ data[0][key2].type +'</td>'
                                +  '<td>'+ arraySum(quantity2) +'</td>'
                                +  '<td>'+ arraySum(priceAll2) +'</td>'
                                +  '<td>'+ data[0][key2].createMan +'</td>'
                                + '</tr>';
                        }

                        list += '</tbody>'
                            + '</table>';
                        break;
                }


                //查询到结果之后
                $(".searchList").html(list);

                //分页
                //设置分页信息
                console.log(data.total_num);
//                var pageOptions = {
//                    AllowPaging: true,
//                    PageIndex: data.this_page,      //设置当前页码
//                    PageSize: data.pageNum,       //设置分页大小
//                    RecordCount: data.total_num,  //设置数据总数
//                    // TotalPage: ,      //设置总页数
//                    showPageCount: 4,
//                    onPageClick: function(pageIndex) {
//                        chooseCheck(type,search,pageIndex,startTime,endTime);
//                    }
//                };
//                //初始化分页栏
//                $('#paging').pageBar(pageOptions);


            },
            error : function (data) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        })

    }





</script>

<script>
//    $(document).ready(function () {
//        //设置分页信息
//        var type = "<?php //echo isset($_GET['type']) ? $_GET['type'] : 'put' ?>//";
//        var pageOptions = {
//            AllowPaging: true,
//            PageIndex: <?php //echo $_GET['page'] ?>//,      //设置当前页码
//            PageSize: <?php //echo $pageNum ?>//,       //设置分页大小
//            RecordCount: <?php //echo $totalNum ?>//,  //设置数据总数
//            // TotalPage: ,      //设置总页数
//            showPageCount: 4,
//            onPageClick: function (pageIndex) {
//                var page = parseInt(pageIndex);
//                window.location.href = "?page=" + page + "&type=" + type;
//            }
//        };
//        //初始化分页栏
//        $('#paging').pageBar(pageOptions);
//    })
</script>
</body>

</html>